// Super Jumping! Jumping! Jumping!
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int i, j, n;
    long long maxsum;
    long long nums[1001] = {0};
    long long sums[1001] = {0};
    while(cin >> n)
    {
        if(n == 0)
        {
            break;
        }
        for(i=0; i<n; i++)
        {
            cin >> nums[i];
            sums[i] = 0;
        }
        maxsum = -1;
        for(i=0; i<n; i++)
        {
            for(j=i; j>=0; j--)
            {
                if(nums[j] < nums[i])
                {
                    sums[i] = max(sums[i], sums[j] + nums[i]);
                }
            }
            sums[i] = max(sums[i], nums[i]);
            if(sums[i] > maxsum)
            {
                maxsum = sums[i];
            }
        }
        sums[i] = max(sums[i], nums[i]);
        cout << maxsum << endl;
    }
}